<style lang="less">
</style>
<template>
  <Modal v-model="show" title="导出任务设置" @on-ok="exportOk">
    <Card>
      <Button type="primary" icon="ios-search"  @click="init">刷新</Button>
      <Table style="margin-top: 3%" border :columns="columns2" :data="dataList2">
      </Table>
      <Row type="flex" justify="end" style="margin-top: 2%">
        <Page :current="pageNo" :total="totalnum" show-sizer :page-size="pageSize"
              show-total @on-change="changePageNo" @on-page-size-change="changePagesize"/>
      </Row>
    </Card>
    <Card style="margin-top: 3%">
      <Row>
        <Col span="4" style="line-height: 2.6;">
          <p>导出任务:</p>
        </Col>
        <Col span="10">
          <Select v-model="export_code" filterable transfer="" @on-change="init">
            <Option :value="item.export_code"  v-for="item in setNameList" v-bind:key="item.export_code">{{item.export_name}}</Option>
          </Select>
        </Col>
      </Row>
      <Table style="margin-top: 3%" border :columns="columns" :data="dataList" @on-selection-change="selection">
        <template slot-scope="{ row, index }" slot="condition">
          <Input v-show="row.export_column_type != 'datetime'" v-model="dataList[index].condition"></Input>
          <DatePicker v-show="row.export_column_type === 'datetime'" v-model="dataList[index].applytime" type="datetimerange"  transfer="" placement="bottom-end"></DatePicker>
        </template>
      </Table>
    </Card>
  </Modal>
</template>
<script>
import { getCountNameList, getSetByUrlList, createExportSetTask, getExportTaskList, date3str } from '@/api/eventlist'
export default {
  name: 'Export',
  data () {
    return {
      export_database: 'worker',
      applytime: [],
      totalnum: 0,
      pageNo: 1,
      pageSize: 10,
      show: false,
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '列名',
          align: 'center',
          key: 'export_column_name'
        },
        {
          title: '条件',
          align: 'center',
          slot: 'condition'
        }
      ],
      columns2: [
        {
          title: '导出编号',
          align: 'center',
          key: 'export_code'
        },
        {
          title: '导出时间',
          align: 'center',
          width: 150,
          key: 'create_time'
        },
        {
          title: '状态',
          align: 'center',
          width: 80,
          key: 'export_state_all'
        },
        {
          title: '操作',
          align: 'center',
          width: 80,
          render: (h, params) => {
            let temp = []
            if (params.row.export_state === '1') {
              temp.push(
                h('Button', {
                  props: { type: 'success', size: 'small' },
                  style: { marginRight: '5px', marginTop: '2px', marginBottom: '2px' },
                  on: {
                    click: () => {
                      window.open(params.row.file_url)
                    }
                  }
                }, '下载'))
            }
            return h('div', temp)
          }
        }
      ],
      dataList: [],
      dataList2: [],
      setNameList: [],
      export_code: '',
      ids: '',
      conditionList: []
    }
  },
  props: {
  },
  methods: {
    init () {
      let params = {
        export_database: this.export_database,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        export_code: this.export_code,
        url_code: this.$route.path.substr(1, this.$route.path.length - 1)
      }
      getCountNameList(params).then(res => {
        if (res.data.errcode === 0) {
          this.dataList = res.data.data.list
        } else {
          this.$Notice.error({
            title: '查询数据失败',
            desc: res.data.errmsg
          })
        }
      })
      getExportTaskList(params).then(res => {
        if (res.data.errcode === 0) {
          this.totalnum = res.data.data.totalRecord
          this.dataList2 = res.data.data.results
          this.dataList2.forEach(function (temp) {
            if (temp.export_state === '0') {
              temp.export_state_all = '未导出'
            } else {
              temp.export_state_all = '已导出'
            }
          })
        } else {
          this.$Notice.error({
            title: '查询数据失败',
            desc: res.data.errmsg
          })
        }
      })
    },
    selection (selection) {
      let that = this
      this.ids = ''
      // selection.forEach(function (temp, index) {
      //   if (index !== 0) {
      //     if (temp.id === row.id) {
      //       let old = selection[index - 1]
      //       selection[index - 1] = temp
      //       selection[index] = old
      //     }
      //   }
      // })
      let array = []
      selection.forEach(function (temp, index) {
        if (temp.condition || (temp.applytime[0] && temp.applytime[0])) {
          console.log(date3str('yyyy-MM-dd hh:mm:ss', temp.applytime[0]))
          for (var i = 0; i < temp.applytime.length; i++) {
            temp.applytime[i] = date3str('yyyy-MM-dd hh:mm:ss', temp.applytime[i])
          }
          let object = {
            'id': temp.id,
            'condition': temp.condition,
            'applytime': temp.applytime
          }
          array.push(object)
        }
        that.ids = that.ids + temp.id + ','
      })
      this.conditionList = array
      this.ids.substring(0, this.ids.length - 1)
      console.log(this.conditionList)
    },
    getSetNameList () {
      let params = {
        export_database: this.export_database,
        url_code: this.$route.path.substr(1, this.$route.path.length - 1)
      }
      getSetByUrlList(params).then(res => {
        if (res.data.errcode === 0) {
          this.setNameList = res.data.data.list
          console.log(this.setNameList.length)
          if (this.setNameList.length === 1) {
            this.export_code = this.setNameList[0].export_code
            this.init()
          }
        } else {
          this.$Notice.error({
            title: '查询数据失败',
            desc: res.data.errmsg
          })
        }
      })
    },
    exportOk () {
      let params = {
        ids: this.ids,
        condition: this.conditionList,
        export_code: this.export_code
      }
      createExportSetTask(params).then(res => {
        if (res.data.errcode === 0) {
          this.$Message.success('导出任务创建成功')
        } else {
          this.$Notice.error({
            title: '导出任务创建失败',
            desc: res.data.errmsg
          })
        }
      })
    },
    show1 (show) {
      this.show = show
    },
    changePagesize (pageSize) {
      this.pageSize = pageSize
      this.changePageNo()
    },
    changePageNo (pageNo) {
      this.pageNo = pageNo
      this.init()
    }
  },
  mounted () {
    this.init()
    this.getSetNameList()
  }
}
</script>
